package com.company.leetcode;

public class Solution31 {
    public void nextPermutation(int[] nums) {
        int n = nums.length-1;
        int i = n;
        while (i>0){
            if (nums[i]>nums[i-1]){
                break;
            }
            i --;
        }
        if (i == 1){
            reverse(nums,0);
            return;
        }
        int j = n;
        while (j>i){
            if (nums[j]>nums[i-1]){
                break;
            }
            j++;
        }
    }

    public static void main(String[] args) {

    }
    //将数列反过来
    private void reverse(int[] nums ,int index){
        int i = index;
        int j = nums.length-1;
        while (i<j){
            swap(i,j,nums);
            i++;
            j--;
        }
    }
    private void swap(int i,int j,int[] nums){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
